package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;

import models.WormChase;

public class WormPanel extends JPanel implements Runnable {
	
	private static final int PWIDTH = 500;
	private static final int PHEIGHT = 400;
	
	private Thread animator;
	
	private volatile boolean gameOver = false;
	private volatile boolean running = false;
	
	private Graphics dbg;
	private Image dbImage = null;
	
	private long period;
	
	public WormPanel(WormChase wc, long period) {
		setBackground(Color.white);
		setPreferredSize(new Dimension(PWIDTH,PHEIGHT));
		setFocusable(true);
		requestFocus();
		readyForTermination();
		
		// create game components
		//wcTop = wc;
		
		// listeners
		addMouseListener( new MouseAdapter() {
			public void mousePressed(MouseEvent e) {  }
		}); 
		
	}
	
	public void startGame() {
		if(animator == null || !running) {
			animator = new Thread(this);
			animator.start();
		}
	}

	public void resumeGame() {
		// TODO Auto-generated method stub
		
	}

	public void pauseGame() {
		// TODO Auto-generated method stub
		
	}

	public void stopGame() {
		running = false;
		
	}

	private void gameOverMessage(Graphics g) {
		//g.drawString(msg, x, y);
	}

	public void readyForTermination() {
		addKeyListener( new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				int keyCode = e.getKeyCode();
				if((keyCode == KeyEvent.VK_ESCAPE)) {
					running = false;
				}
			}
		});
	}

	public void addNotify() {
		super.addNotify();
		startGame();
	}

	private void gameUpdate() {
		if(!gameOver) {
			
		}
	}
	
	private void gameRender() {
		
		if ( dbImage == null) { 
			dbImage = createImage(PWIDTH, PHEIGHT);
			if(dbImage == null) {
				System.out.println("dbImage is null");
				return;
			} else {
				dbg = dbImage.getGraphics();
			}
		}
		
		dbg.setColor(Color.white);
		dbg.fillRect(0, 0, PWIDTH, PHEIGHT);
		
		if(gameOver)
			gameOverMessage(dbg);
		
	}

	public void paintScreen() {
		Graphics g;
		try {
			g = this.getGraphics(); // get the panel graphics
			if( (g != null) && (dbImage != null)) {
				g.drawImage(dbImage,0,0,null);
				Toolkit.getDefaultToolkit().sync(); // sync the display 
				g.dispose();
				
			}
		} catch (Exception e) { System.out.println("Graphics Context Error");}
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		if(dbImage != null) {
			g.drawImage(dbImage, 0, 0, null);
		}
	}

	@Override
	public void run() {
		
		// control the FPS do to the paintscreen method :)
		long beforeTime, timeDiff, sleepTime;
		beforeTime = System.currentTimeMillis();
		
		running = true;
		
		while(running) {
			gameUpdate();
			gameRender();
			paintScreen();
			
			// get the time difference and create the correct sleep time
			timeDiff = System.currentTimeMillis() - beforeTime;
			sleepTime = period - timeDiff;
			
			if( sleepTime <= 0) 
				sleepTime = 5;
			
			try {
				Thread.sleep(sleepTime);
			} catch (InterruptedException ex) {}
			
			beforeTime = System.currentTimeMillis();
		}
		
		System.exit(0);
	}

}
